package com.itheima.ydd.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.ydd.dto.SysConfigDto;
import com.itheima.ydd.dto.SysUserDto;
import com.itheima.ydd.entity.*;
import com.itheima.ydd.mapper.SysConfigMapper;
import com.itheima.ydd.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 */
@Service
@Slf4j
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements ISysConfigService {

    @Autowired
    private SysConfigMapper sysConfigMapper;

    /**
     * 所有配置数据
     *
     * @return
     */
    @Override
    public Map<String, Object> all() {
        Map<String, Object> map = new HashMap<>();
        List<SysConfig> configs = sysConfigMapper.selectList(new LambdaQueryWrapper<SysConfig>()
                .orderByAsc(SysConfig::getSortNum));

        List<SysConfig> allData = new ArrayList<>();
        Map<String, String> configForm = new HashMap<>();
        Map<String, String> configRules = new HashMap<>();

        List<SysConfigDto> dtoList = configs.stream().map(config -> {
            SysConfigDto dto = new SysConfigDto();
            BeanUtils.copyProperties(config,dto);

            //处理 allData
            doConfigValue(dto,config);

            // 处理 configForm
            configForm.put(config.getAttrKey(),config.getAttrValue());

            return dto;
        }).collect(Collectors.toList());

        Map<String, String> rules = doConfigRules(configs);
        configRules.put("site_name",rules.get("site_name"));

        ArrayList<Object> list=new ArrayList<>();
        HashMap<String,Object> map1=new HashMap<>();
        map1.put("name","基础配置");
        map1.put("key","base");

        HashMap<String,Object> map2=new HashMap<>();
        map2.put("name","站点配置");
        LambdaQueryWrapper<SysConfig> lqw=new LambdaQueryWrapper<>();
        lqw.eq(SysConfig::getGroupValue,"站点配置");
        List<SysConfig> list1 = sysConfigMapper.selectList(lqw);
        map2.put("data",list1);

        HashMap<String,Object> map3=new HashMap<>();
        map3.put("name","底部信息");
        LambdaQueryWrapper<SysConfig> lqw1=new LambdaQueryWrapper<>();
        lqw1.eq(SysConfig::getGroupValue,"底部信息");
        List<SysConfig> list2 = sysConfigMapper.selectList(lqw1);
        map3.put("data",list2);

        HashMap<String,Object> map4=new HashMap<>();
        map4.put("name","其他配置");
        LambdaQueryWrapper<SysConfig> lqw2=new LambdaQueryWrapper<>();
        lqw2.eq(SysConfig::getGroupValue,"其他配置");
        List<SysConfig> list3 = sysConfigMapper.selectList(lqw2);
        map4.put("data",list3);

        ArrayList<Map> arrayList=new ArrayList<>();
        arrayList.add(map2);
        arrayList.add(map3);
        arrayList.add(map4);
        map1.put("children",arrayList);
        list.add(map1);

        HashMap<String,Object> map5=new HashMap<>();
        map5.put("name","系统设置");
        map5.put("key","system");
        HashMap<String,Object> map6=new HashMap<>();
        map6.put("name","管理系统配置");
        LambdaQueryWrapper<SysConfig> lqw3=new LambdaQueryWrapper<>();
        lqw3.eq(SysConfig::getGroupValue,"管理系统配置");
        List<SysConfig> list4 = sysConfigMapper.selectList(lqw3);
        map6.put("data",list4);
        ArrayList<Map> arrayList1=new ArrayList<>();
        arrayList1.add(map6);
        map5.put("children",arrayList1);
        list.add(map5);

        HashMap<String,Object> map7=new HashMap<>();
        map7.put("name","其他设置");
        map7.put("key","custom");
        HashMap<String,Object> map8=new HashMap<>();
        map8.put("name","通知公告");
        LambdaQueryWrapper<SysConfig> lqw4=new LambdaQueryWrapper<>();
        lqw4.eq(SysConfig::getGroupValue,"通知公告");
        List<SysConfig> list5 = sysConfigMapper.selectList(lqw4);
        map8.put("data",list5);
        HashMap<String,Object> map9=new HashMap<>();
        map9.put("name","前台图片");
        LambdaQueryWrapper<SysConfig> lqw5=new LambdaQueryWrapper<>();
        lqw5.eq(SysConfig::getGroupValue,"前台图片");
        List<SysConfig> list6 = sysConfigMapper.selectList(lqw5);
        map9.put("data",list6);
        ArrayList<Map> arrayList2=new ArrayList<>();
        arrayList2.add(map8);
        arrayList2.add(map9);
        map7.put("children",arrayList2);
        list.add(map7);

        map.put("allData", dtoList);
        map.put("configForm", configForm);
        map.put("configRules", configRules);
        map.put("list",list);

        return map;
    }

    /**
     * 获取系统明细中的list
     * @return
     */
    @Override
    public List<Map<String, Object>> getList() {
        //设置list
        List<Map<String, Object>> list = new ArrayList<>();
        //list下面3个map,每个map封装name,key,children
        //map1:基础配置
        Map<String, Object> listMap4 = getListMap("base", "基础配置", "站点配置", "底部信息", "其他配置");
        List<Map<String, Object>> children = (List<Map<String, Object>>) listMap4.get("children");
        List<Map<String, Object>> sysConfig125 = get125ConfigValue();

        lo:
        for (Map<String, Object> child : children) {
            List<SysConfig> dataList = (List<SysConfig>) child.get("data");
            for (SysConfig sysConfig : dataList) {
                if (sysConfig.getId() == 125) {
                    sysConfig.setConfigValue(sysConfig125);
                    break lo;
                }
            }
        }

        //map2:系统设置
        Map<String, Object> listMap2 = getListMap("system", "系统设置", "管理系统配置");

        //map3:系统设置
        Map<String, Object> listMap3 = getListMap("custom", "其他设置", "通知公告", "前台图片");

        list.add(listMap4);
        list.add(listMap2);
        list.add(listMap3);

        return list;

    }

    /**
     * 获取list中的map
     *
     * @param key
     * @param name 可变参数--数组
     * @return
     */
    @Override
    public Map<String, Object> getListMap(String key, String... name) {
        Map<String, Object> listMap = new HashMap<>();
        listMap.put("name", name[0]);
        listMap.put("key", key);
        //children:List<Map<String,Object>>
        List<Map<String, Object>> children = new ArrayList<>();
        //childrenMap
        for (int i = 0; i < name.length - 1; i++) {
            Map<String, Object> childrenMap = new HashMap<>();
            String childrenName = name[i + 1];
            childrenMap.put("name", childrenName);

            LambdaQueryWrapper<SysConfig> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SysConfig::getTabValue, key);
            wrapper.eq(SysConfig::getGroupValue, childrenName);
            List<SysConfig> childrenData = this.list(wrapper);

            childrenMap.put("data", childrenData);
            children.add(childrenMap);
        }

        listMap.put("children", children);

        return listMap;

    }

    /**
     * 给id为125的sys_config的ConfigValue赋值
     * @return
     */
    @Override
    public List<Map<String, Object>> get125ConfigValue() {
        List<Map<String, Object>> sysConfig125 = new ArrayList<>();
        Map<String, Object> sysConfig125Map1 = new HashMap<>();
        sysConfig125Map1.put("label", "是");
        sysConfig125Map1.put("value", "1");
        Map<String, Object> sysConfig125Map2 = new HashMap<>();
        sysConfig125Map2.put("label", "否");
        sysConfig125Map2.put("value", "2");
        sysConfig125.add(sysConfig125Map1);
        sysConfig125.add(sysConfig125Map2);
        return sysConfig125;
    }

    private Map<String, String> doConfigRules(List<SysConfig> configs) {
        Map<String, String> ruleHashMap = new HashMap<>();
        for (SysConfig config : configs) {
            if (config.getType()==1){
                String validator = config.getValidator();
                if (!("".equals(validator))){
                    ruleHashMap.put(config.getAttrKey(),validator);
                    break;
                }
            }
        }
        return ruleHashMap;
    }

    /**
     * 处理 ConfigValue
     *
     *
     * @param dto
     * @param config
     * @return
     */
    public SysConfigDto doConfigValue(SysConfigDto dto, SysConfig config) {

        String config_value = (String) config.getConfigValue();
        if (config_value != null && !"".equals(config_value)) {
            Integer type = config.getType();
            if (type == 4) {
                String[] rules = config_value.split("&");
                List<Map<String, String>> configValue = new ArrayList<>();
                for (String rule : rules) {
                    String[] split = rule.split("=");
                    Map<String, String> ruleHashMap = new HashMap<>();

                    ruleHashMap.put("label", split[0]);
                    ruleHashMap.put("value", split[1]);
                    configValue.add(ruleHashMap);
                }
                dto.setConfig_value(configValue);

                return dto;
            }
        }

        return null;
    }
}
